package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.channel.Rank;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/6/29.
 */
public class RankBookAdapter extends RecyclerView.Adapter<RankBookAdapter.RankBookViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<Rank> rankList;


    private mItemOnClickListener itemOnClickListener;

    public RankBookAdapter(Context context, List<Rank> rankList) {
        this.context = context;
        this.rankList = rankList;

        inflater = LayoutInflater.from(context);
    }

    public void setItemOnClickListener(mItemOnClickListener itemOnClickListener)
    {
        this.itemOnClickListener = itemOnClickListener;
    }

    @Override
    public RankBookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.adapter_rank_book,null);
        return new RankBookViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final RankBookViewHolder holder, final int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemOnClickListener.OnItemClick(position);
            }
        });

        int line = position;
        final Rank rank  = rankList.get(position);
        holder.tvtitle.setText(rank.getTitle());
        ImageLoader.getInstance().displayImage(rank.getImage_url(),holder.ivmain, ImageUtil.getDefaultOptions());
        holder.tvbooknum.setText(rank.getSubscribe_num());


        if (line == 0) {
            holder.tvnum.setTextColor(context.getResources().getColor(R.color.firstcolor));
        } else if (line == 1 || line == 2) {
            holder.tvnum.setTextColor(context.getResources().getColor(R.color.colorPublic));
        } else {

            holder.tvnum.setTextColor(context.getResources().getColor(R.color.public_gray));
        }

        holder.tvnum.setText(position+1+"");

        holder.ivadd.setTag(rank.getImage_url());


        LogUtil.w("holder.ivadd.getTag()>>>line" + line  + "》》》"+  holder.ivadd.getTag());

        if( holder.ivadd.getTag() !=null && holder.ivadd.getTag().equals(rank.getImage_url()))
        {
            holder.ivadd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (v.isSelected()) {
                        v.setSelected(false);
                    } else {
                        v.setSelected(true);
                    }
                }
            });
        }




    }

    @Override
    public int getItemCount() {
        return rankList == null ? 0 : rankList.size();
    }

    public class RankBookViewHolder extends RecyclerView.ViewHolder {

        TextView tvnum,tvtitle,tvbooknum;
        ImageView ivmain,ivadd;
        public RankBookViewHolder(View itemView) {
            super(itemView);

            tvnum = (TextView) itemView.findViewById(R.id.adapter_rank_book_num_tv);
            tvbooknum = (TextView) itemView.findViewById(R.id.adapter_rank_book_booknum_tv);
            tvtitle = (TextView) itemView.findViewById(R.id.adapter_rank_book_title_tv);
            ivmain = (ImageView) itemView.findViewById(R.id.adapter_rank_book_main_iv);
            ivadd = (ImageView) itemView.findViewById(R.id.adapter_rank_book_add_iv);

        }
    }

    public interface mItemOnClickListener
    {
        void OnItemClick(int position);
    }
}
